<template>
  <div>
    <!--添加按钮  -->
    <el-button type="primary" icon="el-icon-plus" @click="showDialog"
      >添加</el-button
    >
    <!-- table表格 -->
    <el-table style="width: 100%" :data="trademarkList" border>
      <el-table-column
        prop="id"
        label="序号"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tmName"
        label="品牌名称"
        align="center"
      ></el-table-column>
      <el-table-column prop="prop" label="品牌Logo" align="center">
        <template slot-scope="{ row }">
          <img :src="row.logoUrl" alt="" style="width: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="warning" @click="updataTrademark(row)"
            >修改</el-button
          >
          <el-button type="danger" @click="deleteTrademark(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      style="margin-top: 40px; textalign: center"
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[3, 5, 7]"
      layout="sizes, prev, pager, next, jumper,total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>

    <!-- 对话框 -->
    <el-dialog
      :title="tmForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form ref="tmForm" :model="tmForm" label-width="80px">
        <el-form-item label="品牌名称">
          <el-input v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌logo">
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureTradeMark">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "tradeMark",
  data() {
    return {
      //当前页
      page: 1,
      //总页数
      total: 0,
      //品牌数据
      trademarkList: [],
      //每页展示的数据数量
      limit: 3,
      //控制对话框显示
      dialogVisible: false,
      //添加时收集的表单数据
      tmForm: {
        tmName: "",
        logoUrl: "",
      },
    };
  },
  mounted() {
    // console.log(this.$API);
    this.getTrademarkList();
  },
  methods: {
    async getTrademarkList() {
      // alert(11);
      const { page, limit } = this;
      let result = await this.$API.trademark.reqTradeMarkList(page, limit);
      if (result.code == 200) {
        console.log(result.data);
        this.trademarkList = result.data.records;
        this.total = result.data.total;
      }
    },
    //点击分页器切换页数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getTrademarkList();
    },
    //pageSize 改变时会触发
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.limit = val;
      this.getTrademarkList();
    },
    //关闭对话框
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //上传图片回调
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = URL.createObjectURL(file.raw);
      // console.log(res,file);
    },
    //上传图片之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //点击添加按钮时
    showDialog() {
      this.dialogVisible = true;
      this.tmForm = {
        tmName: "",
        logoUrl: "",
      };
    },
    //确定添加品牌信息
    async sureTradeMark() {
      this.dialogVisible = false;
      let result = await this.$API.trademark.reqTradeMarkAddOrUpate(
        this.tmForm
      );
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: this.tmForm.id ? "修改成功" : "添加品牌成功",
        });
        this.page = this.tmForm.id ? this.page : 1;
        this.getTrademarkList();
      } 
    },
    //修改品牌信息
    updataTrademark(row) {
      this.dialogVisible = true;
      // console.log(row);
      //浅拷贝
      this.tmForm = { ...row };
    },
    //删除品牌
    deleteTrademark(row) {
      console.log(row);
      this.$confirm(`你确定删除${row.tmName}吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let result = await this.$API.trademark.reqDelTradeMark(row.id);
          if (result.code == 200) {
            // console.log(result.data);
            this.getTrademarkList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>